<?php
namespace app\index\domain;

class Classes
{
    private static $_objList;

    /**
     * 获取某个对象
     */
    public static function get($type, $param=null)
    {
        $cacheKey = md5(serialize([$type, $param]));
        if(isset(self::$_objList[$cacheKey])){
            \think\Log::log("$type - $cacheKey : cache hit");
            return self::$_objList[$cacheKey];
        }
        //return $_objList[$cacheKey] = self::create($type, $param);
        self::$_objList[$cacheKey] = self::create($type, $param);
        \think\Log::log("$type - $cacheKey : create");
        return self::$_objList[$cacheKey];
    }

    /**
     * 创建某个对象
     */
    public static function create($type, $param=null)
    {
        switch ($type){
        case 'TermByFlag':
            $id = D('Term')->getIdByFlag($param);
            $obj = new Term($id);
            break;
        case 'TermById':
            $obj = new Term($param);
            break;
        case 'TermListById':
            $obj = new TermList($param);
            break;
        case 'ContentListById':
            $obj = new ContentList($param);
            break;
        case 'ContentById':
            $obj = new Content($param);
            break;
        default:
            $type = "\\app\\index\\domain\\".$type;
            if(!is_null($param))
                $obj = new $type($param);
            else
                $obj = new $type;
        }
        return $obj;
    }
}

